const { Util } = require("../../../utils/util");
const { API_SNZO_URL, API_SNZO_FILE_URL } = require("../../../utils/wxapi");
const wxapi = require("../../../utils/wxapi");
const app = getApp();

// pages/digital/works/works.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuIndex: 'user',
    barTitle: null,
    submenuId: 0,
    specialId:null,
    worksOfData: [],
    sortOfData: [],
    unitOfData: [],
    unitWorksOfData: [],
    listOfData: [],
    specialOfData: [],
    bodyParams: { worksType: '', worksStatus: 1, softDelete: 1, standby6: '' },
    unitBodyParams: { standby1: '', worksType: '', worksStatus: 1, softDelete: 1 },
    specialBodyParams: { standby1: null, artName: null, auhor: null },
    pageParams: { pageNum: 1, pageSize: 16 },
    unitUserPageParams: { pageNum: 1, pageSize: 16 },
    unitWorksPageParams: { pageNum: 1, pageSize: 16 },
    specialWorksPageParams: { pageNum: 1, pageSize: 16 },
    total: 0,
    showUnitList: false,
    showSpecialList: false,
    cacheMenuIndex: 'user',
    worksImgUrl: API_SNZO_FILE_URL,
    isChange: false,
    scrollTop: 0,
    canScroll: false,
  },

  /**
   * 菜单切换
   * @param {*} menuIndex 
   */
  menuChange(e) {
    const menuIndex = e.currentTarget.dataset.menuIndex;
    if (menuIndex === this.data.menuIndex) return;
    if (this.data.isChange) return;
    this.data.isChange = true;
    this.setData({
      menuIndex,
    })
    if (menuIndex === 'user') {
      this.getWorks()
    } else if (menuIndex === 'group') {
      this.getUnit();
    } else if (menuIndex === 'special') {
      this.getSpecialUser()
    }
  },

  /**
   * 二级专业切换
   * @param {*} e 
   */
  sortChange(e) {
    const sortId = e.currentTarget.dataset.sortId;
    if (sortId === this.data.submenuId) return;
    if (this.data.isChange) return;
    this.data.isChange = true;
    this.data.bodyParams.standby6 = sortId != 0 ? sortId : '';
    this.data.pageParams.pageNum = 1;
    this.setData({
      submenuId: sortId
    })
    this.getWorks();
  },

  /**
   * 获取艺术品专业
   */
  getWorksType() {
    wxapi.getStNumWorksType({ standby4: this.data.bodyParams.worksType }).then(res => {
      if (res.status === 200) {
        const sortOfData = res.data.map(item => { item.typeName = item.typeName.slice(0, 2); return item; });
        this.setData({
          sortOfData
        })
      }
    })
  },

  /**
   * 获取艺术品列表
   */
  getWorks() {
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.cacheMenuIndex !== 'user') {
      this.data.pageParams.pageNum = 1;
    }
    this.data.cacheMenuIndex = 'user';
    const params = Util.trimNulls(this.data.pageParams);
    let pageParams = '';
    for (const key in params) {
      if (Object.hasOwnProperty.call(params, key)) {
        const value = params[key];
        pageParams += `&${key}=${value}`
      }
    }
    wxapi.selectByPass(this.data.bodyParams, pageParams).then(res => {
      wx.hideLoading();
      this.data.isChange = false;
      if (res.status === 200) {
        if (this.data.pageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.data;
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          showUnitList: false,
          total: res.data.total,
          canScroll: true
        })
      }
    })
  },

  /**
   * 获取集团用户
   */
  getUnit() {
    if (this.data.cacheMenuIndex !== 'group') {
      this.data.unitUserPageParams.pageNum = 1;
    }
    this.data.cacheMenuIndex = 'group';
    wx.showLoading({
      title: '加载中',
    })
    wxapi.selectUnitAll(this.data.unitUserPageParams).then(res => {
      this.data.isChange = false;
      wx.hideLoading()
      if (res.status === 200) {
        if (this.data.unitUserPageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const listOfData = res.data.seachList;
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          showUnitList: false,
          total: res.data.seachTotal,
          canScroll: true
        })
      }
    })
  },

  /**
   * 获取集团用户艺术品
   */
  getUnitWorks() {
    if (this.data.cacheMenuIndex !== 'group') {
      this.data.unitWorksPageParams.pageNum = 1;
    }
    this.data.cacheMenuIndex = 'group';
    wx.showLoading({
      title: '加载中',
    })
    const params = Util.trimNulls(this.data.unitWorksPageParams);
    let pageParams = '';
    for (const key in params) {
      if (Object.hasOwnProperty.call(params, key)) {
        const value = params[key];
        pageParams += `&${key}=${value}`
      }
    }
    wxapi.selectWorksByExample(this.data.unitBodyParams, pageParams).then(res => {
      this.data.isChange = false;
      wx.hideLoading()
      if (res.status === 200) {
        const listOfData = res.data.data;
        if (listOfData.length === 0) {
          wx.showModal({
            title: '提示信息',
            content: '未找到相关内容',
            showCancel: false
          })
          return;
        }
        if (this.data.unitWorksPageParams.pageNum === 1) {
          this.data.listOfData = [];
          this.setData({
            scrollTop: 0
          })
        }
        const newData = this.data.listOfData.concat(listOfData);
        this.setData({
          listOfData: newData,
          showUnitList: true,
          total: res.data.total,
          canScroll: true
        })
      }
    })
  },

  /**
   * 点击集团用户 获取集团用户艺术品
   * @param {*} e 
   */
  unitClick(e) {
    const standby1 = e.currentTarget.dataset.unitStk;
    this.data.unitBodyParams.standby1 = standby1;
    this.data.unitBodyParams.worksType = this.data.bodyParams.worksType;
    this.getUnitWorks();
  },

  /**
   * 获取专区用户
   */
  getSpecialUser() {
    const listOfData = [
      {
        id: 1,
        typeId: [11],
        unitName: '狮子会哈尔滨专区',
      },
      {
        id: 2,
        typeId: [11],
        unitName: '黑龙江省高校学生油画作品专区',
      },
      {
        id: 3,
        typeId: [24, 12, 7, 11],
        unitName: '齐齐哈尔融媒体中心专区',
      },
      {
        id: 4,
        typeId: [24],
        unitName: '龙江行有奖大赛活动艺术品专区',
      },
    ];
    const specialOfData = listOfData.filter((item) => {
      if (item.typeId.indexOf(this.data.bodyParams.worksType) !== -1) {
        return item;
      } else {
        return;
      }
    });
    this.data.isChange = false;
    this.setData({
      showUnitList: false,
      showSpecialList: false,
      specialOfData,
      total: listOfData.length
    })
  },

  /**
   * 获取专区艺术品
   */
  getSpecialWorks() {
    if (this.data.cacheMenuIndex !== 'special') {
      this.data.specialWorksPageParams.pageNum = 1;
    }
    this.data.cacheMenuIndex = 'special';
    wx.showLoading({
      title: '加载中',
    })
    const params = Util.trimNulls(this.data.specialWorksPageParams);
    let pageParams = '';
    for (const key in params) {
      if (Object.hasOwnProperty.call(params, key)) {
        const value = params[key];
        pageParams += `&${key}=${value}`
      }
    }
    if (this.data.specialBodyParams.standby1 == 1 || this.data.specialBodyParams.standby1 == 2) {
      const bodyParams = {
        standby1: this.data.specialBodyParams.standby1,
        artStatus: 1,
        artName: this.data.specialBodyParams.artName
      }
      wxapi.getArtShowMsgByPayStatus(bodyParams, this.data.specialWorksPageParams).then(res => {
        this.data.isChange = false;
        wx.hideLoading()
        if (res.status === 200) {
          const listOfData = res.data.seachList;
          if (listOfData.length === 0) {
            wx.showModal({
              title: '提示信息',
              content: '未找到相关内容',
              showCancel: false
            })
            return;
          }
          if (this.data.specialWorksPageParams.pageNum === 1) {
            this.data.listOfData = [];
            this.setData({
              scrollTop: 0
            })
          }
          const newData = this.data.listOfData.concat(listOfData);
          this.setData({
            showUnitList: false,
            showSpecialList: true,
            listOfData: newData,
            total: res.data.seachTotal,
            canScroll: true
          })
        }
      })
    } else if (this.data.specialBodyParams.standby1 == 4) {
      const bodyParams = {
        contest: 'LS',
        worksType: this.data.bodyParams.worksType
      }
      wxapi.selectByPass(bodyParams, pageParams).then(res => {
        this.data.isChange = false;
        wx.hideLoading()
        if (res.status === 200) {
          const listOfData = res.data.data;
          if (listOfData.length === 0) {
            wx.showModal({
              title: '提示信息',
              content: '未找到相关内容',
              showCancel: false
            })
            return;
          }
          if (this.data.specialWorksPageParams.pageNum === 1) {
            this.data.listOfData = [];
            this.setData({
              scrollTop: 0
            })
          }
          const newData = this.data.listOfData.concat(listOfData);
          this.setData({
            showUnitList: false,
            showSpecialList: true,
            listOfData: newData,
            total: res.data.total,
            canScroll: true
          })
        }
      })
    }else{
      wx.hideLoading()
    }
  },

  /**
   * 点击专区 获取专区艺术品
   * @param {*} e 
   */
  specialClick(e) {
    const specialId = e.currentTarget.dataset.specialId;
    this.data.specialBodyParams.standby1 = specialId;
    this.setData({
      specialId
    })
    this.getSpecialWorks();
  },

  /**
   * 加载更多
   */
  scrollToLower() {
    if (this.data.listOfData.length === this.data.total) {
      return;
    }
    this.setData({
      canScroll: false
    })
    setTimeout(() => {
      if (this.data.cacheMenuIndex === 'user') {
        this.data.pageParams.pageNum++
        this.getWorks();
      } else if (this.data.cacheMenuIndex === 'group') {
        if (this.data.showUnitList) {
          this.data.unitWorksPageParams.pageNum++
          this.getUnitWorks();
        } else {
          this.data.unitUserPageParams.pageNum++
          this.getUnit();
        }
      }else if(this.data.cacheMenuIndex === 'special'){
        if (this.data.showSpecialList) {
          this.data.specialWorksPageParams.pageNum++
          this.getSpecialWorks();
        }
      }
    }, 1000);
  },

  /**
   * 预览艺术品
   * @param {*} e 
   */
  previewWorks(e) {
    const worksInfo = e.currentTarget.dataset.works;
    const worksType = worksInfo.tabStnumWorkses[0].standby2;
    if (worksType.includes('video')) {
      wx.previewMedia({
        sources: [
          {
            url: this.data.worksImgUrl + worksInfo.tabStnumWorkses[0].standby1,
            type: 'video',
            poster: this.data.worksImgUrl + worksInfo.tabStnumWorksMsg.worksPhoto
          }
        ],
      })
    } else if (worksType.includes('image')) {
      wx.previewMedia({
        sources: [
          {
            url: this.data.worksImgUrl + worksInfo.tabStnumWorkses[0].standby1,
            type: 'image'
          }
        ],
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const DigitalTitle = {
      music: {
        title: '音乐',
        belongLableid: 11,
        worksTypeId: 7
      },
      calligraphy: {
        title: '书法',
        belongLableid: 12,
        worksTypeId: 12
      },
      painting: {
        title: '绘画',
        belongLableid: 13,
        worksTypeId: 11
      },
      photography: {
        title: '摄影',
        belongLableid: 18,
        worksTypeId: 24
      },
    }
    const barTitle = DigitalTitle[options.type].title;
    wx.setNavigationBarTitle({
      title: `神通${barTitle}艺术品展示`
    })
    this.data.bodyParams.worksType = DigitalTitle[options.type].worksTypeId;
    this.setData({
      bodyParams: this.data.bodyParams,
      barTitle
    })
    this.getWorks();
    this.getWorksType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})